<template>
  <div>
    <div style="padding-top: 120px">
      <div class="login-box">
        <div class="logo">
          <img src="../assets/imgs/logo.png" alt="易学啦" />
        </div>
        <form class="el-form">
          <div class="el-form-item el-form-item--feedback">
            <div class="el-form-item__content">
              <div class="login-input el-input el-input--prefix">
                <input
                  type="text"
                  placeholder="请输入Email"
                  class="el-input__inner"
                  v-model="Email"
                />
                <span class="el-input__prefix">
                  <i class="el-input__icon el-icon-user-solid"></i>
                </span>
              </div>
            </div>
          </div>
          <div class="el-form-item el-form-item--feedback">
            <div class="el-form-item__content">
              <div class="login-input el-input el-input--prefix">
                <input
                  type="text"
                  placeholder="请输入昵称"
                  class="el-input__inner"
                  v-model="userName"
                />
                <span class="el-input__prefix">
                  <i class="el-input__icon el-icon-user-solid"></i>
                </span>
              </div>
            </div>
          </div>
          <div class="el-form-item el-form-item--feedback is-required">
            <div class="el-form-item__content">
              <div class="login-input el-input el-input--prefix">
                <input
                  type="text"
                  placeholder="请输入密码"
                  class="el-input__inner"
                  v-popover:popover
                  v-model="PassWord"
                />
                <el-popover
                  ref="popover"
                  placement="right"
                  title="密码要求"
                  width="250"
                  trigger="focus"
                  content="以字母开头，长度在6~18之间，只能包含字母、数字和下划线"
                >
                </el-popover>
                <span class="el-input__prefix">
                  <i class="el-input__icon el-icon-lock"></i>
                </span>
                <span class="el-input__suffix">
                  <span class="el-input__suffix-inner"></span>
                </span>
              </div>
            </div>
          </div>
        </form>
        <div class="box-sign">
          <span
            >已有账号，请
            <router-link to="/login">
              <span class="green">登录</span>
            </router-link>
          </span>
        </div>
        <button type="button" class="el-button sign-btn" @click="submit">
          <span>注册</span>
        </button>

        <!-- 其他登录方式 -->
        <div class="login-other">
          <div class="other-title">
            <span></span>
            <p>其他登录方式</p>
            <span></span>
          </div>
          <div class="other-icon">
            <div class="login-icon-item">
              <i class="iconfont icon-weixin-copy"></i>
              <span>微信登录</span>
            </div>
            <div class="login-icon-item">
              <i class="iconfont icon-qq"></i>
              <span>QQ登录</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      snginlists: null,
      uplists: { userlists: [] },
      Email: "",
      PassWord: "",
      userName: "",
    };
  },
  methods: {
    submit() {
      if (this.Email == "" || this.PassWord == "" || this.userName == "") {
        this.$message.error("请填写完整信息");
        return;
      }

      let reg = /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/;
      let res = /^[a-zA-Z]\w{5,17}$/; // 密码(以字母开头，长度在6~18之间，只能包含字母、数字和下划线)
      // console.log("邮箱",reg.test(this.Email));
      // console.log(res.test(this.PassWord));

      // 1417328083@qq.com
      // 判断信息是否完整
      if (!reg.test(this.Email) || !res.test(this.PassWord)) {
        this.$message.error("请填写正确的信息");
        return;
      }

      //  this.snginlists = JSON.parse()
      this.snginlists = JSON.parse(localStorage.getItem("loginlist")).userlists; //获取数据列表
      let index = this.snginlists.some(
        (item) => item.userName == this.userName
      ); //查找是否有相同ID
      let userEmails = this.snginlists.some(
        (item) => item.userEmail == this.Email
      ); //查找是否有相同邮箱

      console.log("loginlist", JSON.parse(localStorage.getItem("loginlist")));
      console.log(
        "snginlists",
        JSON.parse(localStorage.getItem("loginlist")).userlists
      );
      console.log("userEmails", userEmails);
      console.log("index", index);
      if (index) {
        this.$message.error("该用户名已存在");
        return;
      } else if (userEmails) {
        this.$message.error("该邮箱已注册");
        return;
      }

      //将数据按结构保存并且上传到终端
      // "userheader": "../assets/imgs/user-header.png" 自动上传默认头像
      let userlists = {
        userName: this.userName,
        userEmail: this.Email,
        userPassword: this.PassWord,
        userheader: "../assets/imgs/user-header.png",
      };
      this.snginlists.push(userlists);

      localStorage.setItem("userList", JSON.stringify(userlists));
      this.uplists.userlists = this.snginlists; //将数据按结构保存
      localStorage.setItem("loginlist", JSON.stringify(this.uplists));

      // 成功注册
      this.$message({
        showClose: true,
        message: "注册成功，正在跳转登录页面",
        type: "success",
      });

      setTimeout(() => {
        this.$router.push("login");
      }, 1000);
    },
  },
  created() {},
};
</script>

<style lang="scss" scoped>
* {
  padding: 0px;
  margin: 0px;
}
body {
  width: 100%;
  height: 100%;
  background: url(../assets/imgs/login.d742ee1.png) no-repeat;
  background-size: 100%;
}
.cursor {
  cursor: pointer;
}
a {
  text-decoration: none;
  color: #333;
}
.login-box {
  width: 340px;
  background: #fff;
  box-shadow: 0 2px 40px 0 rgb(0 0 0 / 7%);
  border-radius: 10px;
  padding: 40px 60px;
  margin: 0 auto;
  .login-input {
    margin-bottom: 15px;
    margin-bottom: 15px;
  }
  .box-sign {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    font-weight: 400;
    color: #333;
    line-height: 20px;
  }
  .green {
    color: #48c5a3;
  }
  .sign-btn {
    width: 100%;
    margin-top: 25px;
    color: #fff;
    background: #48c5a3;
    border: none;
  }
  .other-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 40px;
    span {
      width: 124px;
      border-bottom: 1px solid #ddd;
      width: 124px;
      border-bottom: 1px solid #ddd;
    }
    p {
      font-size: 12px;
      font-weight: 400;
      color: #999;
      line-height: 17px;
    }
  }
  .other-icon {
    display: flex;
    justify-content: space-around;
    align-items: center;
    margin-top: 12px;
    .login-icon-item {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      i {
        width: 50px;
        height: 50px;
        font-size: 50px;
        color: #e8e8e8;
        &:hover {
          color: #48c5a3;
        }
      }
      span {
        margin-top: 10px;
        color: #999;
      }
    }
  }
}
.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 80px;
  img {
    width: 90px;
    height: 28px;
  }
}
.active-link {
  color: #48c5a3;
}
form {
  display: block;
  margin-top: 0em;
}
.el-form-item {
  margin-bottom: 22px;
}
.el-form-item__content {
  line-height: 40px;
  position: relative;
  font-size: 14px;
}
.el-input {
  position: relative;
  font-size: 14px;
  display: inline-block;
  width: 100%;
  position: relative;
  font-size: 14px;
  display: inline-block;
  width: 100%;
}
.el-input--prefix {
  .el-input__inner {
    padding-left: 30px;
  }
}
.el-input__inner {
  background-color: #fff;
  background-image: none;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  box-sizing: border-box;
  color: #606266;
  display: inline-block;
  font-size: inherit;
  height: 40px;
  line-height: 40px;
  outline: 0;
  padding: 0 15px;
  width: 100%;
}
.el-input__prefix {
  left: 5px;
  transition: all 0.3s;
}
.el-input__prefix,
.el-input__suffix {
  position: absolute;
  top: 0;
  height: 100%;
  color: #c0c4cc;
  text-align: center;
}
.el-input__icon {
  height: 100%;
  width: 25px;
  text-align: center;
  transition: all 0.3s;
  line-height: 40px;
}
[class*=" el-icon-"],
[class^="el-icon-"] {
  font-family: element-icons !important;
  font-style: normal;
  font-weight: 400;
  font-feature-settings: normal;
  font-variant: normal;
  text-transform: none;
  line-height: 1;
  vertical-align: baseline;
  display: inline-block;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
.el-button {
  display: inline-block;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  background: #fff;
  border: 1px solid #dcdfe6;
  color: #606266;
  text-align: center;
  box-sizing: border-box;
  outline: 0;
  margin: 0;
  transition: 0.1s;
  font-weight: 500;
  padding: 12px 20px;
  font-size: 14px;
  border-radius: 4px;
}
</style>